﻿using UnityEditor;
using UnityEngine;
using System.Collections;

[CustomEditor(typeof(PUEXFanEmitter))]
public class PUEXFanEmitterEditor : Editor {

	public void OnSceneGUI ()
	{
		var affector = target as PUEXFanEmitter;
		if (!affector.enabled)
			return;
		
		Matrix4x4 defaultMatrix = Handles.matrix;
		Handles.matrix = affector.transform.localToWorldMatrix;

		Quaternion quat = Quaternion.Euler (-affector.directionAngle, 0, 0);
		Vector3 center = Vector3.forward * affector.radius / 2f;
//		if (Tools.current == Tool.Rotate) {
//			quat = Handles.RotationHandle (quat, center);
//			
//			if (GUI.changed) {
//				Undo.RecordObject(affector, "PUEXFanEmitterEditor Modify");
//				affector.directionAngle = -quat.eulerAngles.x;
//				EditorUtility.SetDirty(affector);
//			}
//		}

		Vector3 dir = center + quat * Vector3.forward * HandleUtility.GetHandleSize(center);
		Handles.DrawLine (center, dir);
		Handles.ConeCap(0, dir, quat, HandleUtility.GetHandleSize(center) * 0.2f);

		Color defaultColor = Handles.color;
		Handles.color = Color.blue;

		float a = (affector.angle * Mathf.Deg2Rad / 2f);
		float x = Mathf.Cos (a) * affector.radius;
		float y = Mathf.Sin (a) * affector.radius;
		Handles.DrawWireArc(Vector3.zero, Vector3.up, new Vector3(y, 0f, x), -affector.angle, affector.radius);

		Handles.DrawLine (Vector3.zero, new Vector3(y, 0f, x));
		a = a - (affector.angle * Mathf.Deg2Rad);
		x = Mathf.Cos (a) * affector.radius;
		y = Mathf.Sin (a) * affector.radius;
		Handles.DrawLine (Vector3.zero, new Vector3(y, 0f, x));

		Handles.color = defaultColor;
		Handles.matrix = defaultMatrix;
	}

}
